//
//  SYJRetweetView.swift
//  weibo
//
//  Created by 孙英建 on 16/4/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class SYJRetweetView: UIView {

    // 记录转发微博的底部
    var retweetViewConstraint: Constraint?

    var statusModel: SYJStatusesModel?{
        didSet{
            contentLabel.text = statusModel?.retweeted_status?.text
            
            // 卸载之前的自身约束
            self.retweetViewConstraint?.uninstall()

            // 判断是否有微博配图
            if statusModel?.retweeted_status?.pic_urls?.count != 0 {
                
                pictureView.pictureInfo = statusModel?.retweeted_status?.pic_urls
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                // 显示配图
                pictureView.hidden = false
            }else{
                // 无配图
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                pictureView.hidden = true
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 设置UI
    func setUI(){
        
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(10)
            make.width.equalTo(kWidth - 20)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo(contentLabel)
        }
        
        // 自身的约束
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
    }
    
    // MARK: - 懒加载控件
    
    // 转发微博正文
    private lazy var contentLabel: UILabel = {
       let cl = UILabel()
        cl.textColor = UIColor.blackColor()
        cl.font = UIFont.systemFontOfSize(14)
        cl.numberOfLines = 0
        return cl
    }()
    
    // 转发微博配图
    private lazy var pictureView: SYJPictureView = SYJPictureView()
    
}















